<template>
  <div>
    <van-nav-bar
      title="登录"
      left-arrow
      @click-left="$router.back()"
      class="tvar-c"
    />
    <van-form @submit="onSubmit" submit-on-enter ref="form">
      <van-field
        v-model="user.mobile"
        icon-prefix="toutiao"
        clearable
        name="mobile"
        left-icon="shouji"
        placeholder="请输入手机号名"
        :rules="rules.mobile"
      />
      <van-field
        v-model="user.code"
        icon-prefix="toutiao"
        clearable
        left-icon="yanzhengma"
        placeholder="请输入验证码"
        :rules="rules.code"
      >
        <template #button>
          <van-button
            v-if="isShow"
            native-type="button"
            size="small"
            type="default"
          >
            <van-count-down :time="3 * 1000" @finish="isShow = false" />
          </van-button>
          <van-button
            native-type="button"
            @click="sendSms"
            size="small"
            v-else
            round
            type="primary"
            class="yzm-color"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Login, Sms } from "@/api/user";
export default {
  name: "login",

  data() {
    return {
      isShow: false,
      user: {
        mobile: "18612345678",
        code: "246810",
      },
      rules: {
        mobile: [
          { required: true, message: "请输入手机号名" },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: "手机号格式错误",
          },
        ],
        code: [
          { required: true, message: "请输入验证码" },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误",
          },
        ],
      },
    };
  },

  mounted() {},

  methods: {
    // 登录
    async onSubmit() {
      this.$toast.loading({
        message: "登陆中...",
        forbidClick: true,
      });
      let res
      try {
       res = await Login(this.user);
        console.log(res);
      } catch (error) {
        this.$toast.fail("登录失败");
        // return 出去就可以执行下面的代码
        return console.log(error);
      }
       const {data} = res
      this.$store.commit('setUser', data.data);
      this.$router.replace('/my')
      this.$toast.success("登录成功");
    },
    // 发送验证码事件
    async sendSms() {
      try {
        // console.log(this.$refs.form.validate);
        this.$refs.form.validate("mobile");
      } catch (error) {
        this.$toast.fail("手机格式不正确");
      }
      // 3. 请求发送验证码
      try {
        await Sms(this.user.mobile);
        this.$toast("发送成功");
      } catch (err) {
        // 发送失败，关闭倒计时，显示发送按钮
        this.isShow = false;
        if (err.response.status === 429) {
          this.$toast("发送太频繁了，请稍后重试");
        } else {
          this.$toast("发送失败，请稍后重试");
        }
      }
     
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .toutiao {
  font-size: 40px;
}
.yzm-color {
  background: rgb(238, 238, 238);
  color: #000;
}
</style>
